package com.jinxin.phonecloudmusic.myapplication.utils;

import android.support.annotation.NonNull;
import android.util.Log;

import com.jinxin.phonecloudmusic.myapplication.event.MusicControlEvent;

import io.reactivex.Flowable;
import io.reactivex.processors.FlowableProcessor;
import io.reactivex.processors.PublishProcessor;

public class RxBus {
	private FlowableProcessor<Object> mBus;

	private RxBus() {
		mBus = PublishProcessor.create().toSerialized();
	}

	private static RxBus instance;

	public static RxBus getDefault() {
		if(instance==null){
			instance = new RxBus();
		}
		return instance;
	}

	public void post(@NonNull Object obj) {
		mBus.onNext(obj);
//		if (obj instanceof MusicControlEvent){
//			if (((MusicControlEvent) obj).getTag()==9){
//				Log.e("----9",9+"");
//			}
//		}
	}

	public <T> Flowable<T> register(Class<T> clz) {
		return mBus.ofType(clz);
	}

	public void unregisterAll() {
		//解除注册
		mBus.onComplete();
		instance = null;
	}

	public boolean hasSubscribers() {
		return mBus.hasSubscribers();
	}
}
